using System.Collections;
using System.IO;
using LitJson;
using System.Collections.Generic;
using UnityEngine;

public class data_manager {
	
	Dictionary<string, JsonData> data = new Dictionary<string, JsonData>(); 

	public data_manager(){}
	
	public JsonData get_data(string fileName)
	{
		if(!data.ContainsKey(fileName))
		{
			string filePath = "assets/json/"+fileName+".json";
			JsonData new_data = LoadJsonFile(filePath);
			data.Add(fileName, new_data);
		}	
		return data[fileName];		
	}
	JsonData LoadJsonFile(string fpath)
	{
		StreamReader sr = null;
		try {  
	        sr = File.OpenText(fpath);  
	    } catch (System.Exception ex) {  
	        ex.ToString();  
	        return null;  
	    }  

		string strInfo = sr.ReadToEnd();
		//关闭流  
	    sr.Close();  
	    //销毁流  
	    sr.Dispose(); 
		string testStr = "{'1':['a','b','c']}";
		LitJson.JsonData jsdata = LitJson.JsonMapper.ToObject(testStr);
		return jsdata;
		/*
		foreach (DictionaryEntry item in jsdata)
		{
			print (item.Key.ToString());
			//JsonData jdata = LitJson.JsonMapper.ToObject(item.Value.ToString());
			//print (jdata.ToJson());
			JsonData jdata = jsdata[item.Key.ToString()];
			print (jdata.ToJson());
		}*/		
	}
	
	bool isMember(LitJson.JsonData jdata , int key)
	{
		try{
			if(jdata.GetJsonType()==JsonType.Array && key<jdata.Count && key>=0){
				return true;				
			}else if(jdata.GetJsonType()==JsonType.Object){
				foreach (DictionaryEntry item in jdata)
				{
					if(item.Key.ToString() == key.ToString()){
						return true;
					}
				}
			}
			return false;
		}catch(System.Exception ex){
			return false;
		}
	}
	
}

//#define g_data_manager singleton<data_manager>::get_single_item()
